package com.ucode.wap.cgms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.ucode.cgms.service.CgmsCompanionUserService;
import com.ucode.cgms.service.CgmsJointServiceUserService;
import com.ucode.model.cgms.CgmsCompanionUser;
import com.ucode.model.cgms.CgmsJointServiceUser;
import com.ucode.model.cgms.convert.CgmsJointServiceUserConvert;
import com.ucode.model.cgms.form.CgmsJointServiceUserAddForm;
import com.ucode.model.cgms.vo.CgmsJointServiceUserVO;
import com.ucode.tool.annotation.NeedLogin;
import com.ucode.tool.base.ResponseResult;
import com.ucode.tool.util.CollectionUtils;
import com.ucode.wap.base.core.CurrentLoginUserHandler;
import com.ucode.wap.cgms.service.ApiCgmsCompanionOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Tag(name = "移动端 - 陪玩社区系统 - 订单联合服务人")
@RestController
@RequestMapping("/cgms/joint-service-user")
@Validated
public class ApiCgmsJointServiceUserController {

    private static Log log = LogFactory.get();

    @Resource
    private ApiCgmsCompanionOrderService apiCgmsCompanionOrderService;
    @Resource
    private CgmsJointServiceUserService cgmsJointServiceUserService;
    @Resource
    private CgmsCompanionUserService companionUserService;
    @Resource
    private CurrentLoginUserHandler currentLoginUserHandler;

    @NeedLogin(validate = true)
    @Operation(summary = "订单联合服务人员列表")
    @GetMapping(value = "/list/{orderId}")
    public ResponseResult<List<CgmsJointServiceUserVO>> list(@PathVariable(name="orderId",required=true) Long orderId) {

        List<CgmsJointServiceUser>  list = cgmsJointServiceUserService.queryListByOrderId(orderId);
        List<CgmsJointServiceUserVO> voList = CgmsJointServiceUserConvert.INSTANCE.convertList(list);
        if(CollUtil.isNotEmpty(voList)){
            Set<Long> serviceUserIds =  CollectionUtils.convertSet(voList, CgmsJointServiceUserVO::getServiceUserId);
            Map<Long, CgmsCompanionUser>  companionUserMap = companionUserService.getMap(serviceUserIds);
            voList.forEach(vo -> {
                CgmsCompanionUser companionUser = companionUserMap.get(vo.getServiceUserId());
                if(ObjectUtil.isNotEmpty(companionUser)){
                    vo.setNickname(companionUser.getNickName());
                    vo.setAvatar(companionUser.getAvatar());
                }
            });

        }
        return ResponseResult.success(voList);
    }

    @NeedLogin(validate = true)
    @PostMapping("/add")
    @Operation(summary = "添加联合服务人")
    public ResponseResult<Long> submit(@Valid @RequestBody CgmsJointServiceUserAddForm form) {
        apiCgmsCompanionOrderService.addJointServiceUser(form);
        return ResponseResult.success();
    }

    @NeedLogin(validate = true)
    @PostMapping("/delete/{id}")
    @Parameter(description = "编号", name = "id", required = true, in = ParameterIn.PATH, example = "1024")
    @Operation(summary = "删除联合服务人")
    public ResponseResult delete(@PathVariable(name = "id", required = true) Long id) {
        Long userId = currentLoginUserHandler.getCurrentUserId();
        cgmsJointServiceUserService.delete(userId,id);
        return ResponseResult.success();
    }


}
